﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;


namespace Football_Application
{

    class ChampionshipRepository
    {
      
        private String _fileName;
        public ChampionshipRepository(String fileName)
        {
            _fileName = fileName;
            ReadInstance(fileName);
        }

        private List<Championship> _champList;
        public List<Championship> ChampList 
        {
            get { return _champList; }
        }

        public ChampionshipRepository()
            {
               this._champList = new List<Championship>() ;
            }

        public void CreateChampionship(Championship newChamp)
        {
            _champList.Add(newChamp);
        }

        public void ReadInstance(string fileName)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(List<Championship>));
            using (TextReader textReader = new StreamReader(fileName))
            {
                _champList  = (List<Championship>)deserializer.Deserialize(textReader);
            }
        }

        public void WriteInstance(string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Championship >));
            using (TextWriter textWriter = new StreamWriter(fileName))
            {
                serializer.Serialize(textWriter,_champList );
            }
        }

        public override string ToString()
        {
            string result = "";
            foreach (Championship ch   in _champList  )
                result += ch + "\n";
            return result;
        }
    }
}
